   <div id ="planLaryer" style="display: none;">
    <nav ui-nav class="navi" ng-include="'src/app/channel/recordingTask/planLaryer.html'"></nav>
  </div>

  <div class="bg-light lter b-b wrapper-md">
    <h1 class="m-n font-thin h3">录制任务管理</h1>
  </div>
  <div class="wrapper-md">
    <div class="row">

    </div>

    <div class="col-sm-12">
      <div class="panel panel-default">

        <div  ng-controller="dataTablesCtrl">
         <div class="row">

           <div class="col-sm-12" >
             <div class="panel-heading">
              <span>频道:</span>
              <input type="text" name="channelName" id="channelName" readonly="true">
              <br/>
              <a href="javascript:void(0);" ><button class="btn btn-default btn-sm "><i class="fa glyphicon-sort-by-alphabet-alt fa-lg"></i>&nbsp; 实时任务</button></a>

              <a href="javascript:void(0);" ><button class="btn btn-default btn-sm"><i class="fa glyphicon-sort-by-attributes-alt fa-lg"></i>&nbsp;定时任务</button></a>

              <a href="javascript:void(0);" ng-click="realTask();" ><button class="btn btn-default btn-sm "><i class="fa fa-edit fa-lg"></i>&nbsp; 新增实时任务</button></a>

              <a href="javascript:void(0);" ng-click="timeTask();" ><button class="btn btn-default btn-sm "><i class="fa fa-edit fa-lg"></i>&nbsp; 新增定时任务</button></a>

              <a href="javascript:void(0);" ng-click="goRemove();" ><button class="btn btn-default btn-sm "><i class="fa fa-trash-o fa-lg"></i>&nbsp; 删除</button></a>

              <!--隐藏域-->
              <input type="hidden" id ="optIds" name="optIds">


            </div>
          </div>
        </div>
        <table id="myTable" class="table table-striped m-b-none">
        </table>
      </div>
    </div>
  </div>

</div>

</div>


<!--样式-->
<style type="text/css">  
  div#rMenu {position:absolute; visibility:hidden; top:0; background-color: #555;text-align: left;padding: 2px;}  
  div#rMenu a{  
    cursor: pointer;  
    list-style: none outside none;  
  }  

  .selected {
    background-color: #DFF0D8;
  }

  /* dataTables列内容居中 */
  .table>tbody>tr>td{
    text-align:center;
  }

  /* dataTables表头居中 */
  .table>thead:first-child>tr:first-child>th{
    text-align:center;
  }
  #urlDate{
    padding-top: 80px;
    padding-left: 40px;
  }


</style>
<!--样式-->


<script type="text/javascript">

//angularjs总控制器
//频道ID

app.controller('dataTablesCtrl', function($scope, $http,$state,$stateParams) {

 var channelId = $stateParams.channelId;
 $scope.realTask = function(){
  top.layer.open({
    type: 1,
    title: '新建实时任务',
    fix: false,
        maxmin: true, //开启最大化最小化按钮
        shadeClose: true,
        shift: 4,
        shade: [0.5],
        //skin: 'layui-layer-rim',边框
        move: false,
        area: ['600px', '510px'],
        content: $('#planLaryer'),
        yes: function (index, layero) {
          $scope.contact = null;
          $scope.father = null;
        },
        no: function(){
         $scope.contact = null;
         $scope.father = null;
       }
     });
}

 //===========================================================================
 $scope.timeTask = function(){
/*  top.layer.open({
  type: 1,
  title: '新建定时任务',
  fix: false,
        maxmin: true, //开启最大化最小化按钮
        shadeClose: true,
        shift: 4,
        shade: [0.5],
        //skin: 'layui-layer-rim',边框
        move: false,
        area: ['600px', '510px'],
        content: $('#timedTask'),
        yes: function (index, layero) {
          $scope.contact = null;
          $scope.father = null;
        },
        no: function(){
         $scope.contact = null;
         $scope.father = null;
       }
     });*/

     $state.go('app.channel.timedTask',{channelId:channelId});
   }
 //=====================================================================
//进入编辑页面
$scope.goEdit = function () {
  var optIds = $('#optIds').val();
  if(''==optIds){
    top.layer.msg('请至少选择一项', {icon: 6});
    return false;
  }
  if(optIds.indexOf(",") != -1){
    top.layer.msg('编辑只能选择一项', {icon: 6});
    return false;
  }
  $state.go('app.stream.streamEdit', {infoId: optIds});
};


//进入编辑页面
$scope.goRemove = function () {
 var optIds = $('#optIds').val();
 if(''==optIds){
  top.layer.msg('请至少选择一项', {icon: 6});
  return false;
}

top.layer.confirm('您确定删除这些信息吗', function(index){

  var postInfoUrl = API_URL+"task_record"; 

  var optArrs = optIds.split(',');

  for(var i = 0;i< optArrs.length;i++ ){

   var infoUrl = API_URL+"task_record?query=delflag:0,Id:"+optArrs[i];

   var formData =  MY_ASYNC_AJAX(infoUrl)[0];

   formData.Delflag = "1";

   $.ajax({
     url : postInfoUrl+'/'+optArrs[i],
     type : 'put',
     data: JSON.stringify(formData),
     cache : false,
     success : function(data) {

     }
   });

 }
 top.layer.msg('删除成功');
 $('#myTable').DataTable().ajax.reload(); 
});
};

});

function checkOne(){
  var optIds = '';
  $("input[name='ids']:checkbox:checked").each(function(){ 
    optIds+=$(this).val() +',';
  });
  if(''!=optIds){
    optIds = optIds.substring(0,optIds.length-1);
  }
  $('#optIds').val(optIds);

  console.log($('#optIds').val());
}

function checkAll(){
  var checklist = document.getElementsByName("ids");
  var checkFather = document.getElementById("allChecked");
  if(checkFather.checked){
    for(var i=0;i<checklist.length;i++){
      checklist[i].checked = true;
    }
  }else {
    for (var j= 0; j < checklist.length; j++) {
      checklist[j].checked = false;
    }
  }

  var optIds ='';
  if(checkFather.checked){
    $("input[name='ids']:checkbox:checked").each(function(index, el) {
      optIds+=$(this).val()+',';
      
    });
  }else {
    optIds='';
  }
  //这里是为了去掉 最后一个逗号
  if(optIds!=null&&optIds!=''){
    optIds=optIds.substring(0, optIds.length-1);
  }

  $("#optIds").val(optIds);
  console.log($("#optIds").val());
}




$(document).ready(function(){


 var url =  document.URL;
 var arr =  url.split("/");
 channelId = arr[arr.length-1];
 var channelUrl = API_URL+"channel_info?query=delflag:0,Id:"+channelId;

 var Channel = MY_ASYNC_AJAX(channelUrl)[0];

 $("#channelName").val(Channel.ChannelName);


 var dtUrl =  API_URL+"task_record?query=delflag:0,ChannelId:"+channelId;
 //初始化dataTable
 var table =  $('#myTable').DataTable({
  ajax: {  
    url: dtUrl,  
    success:function(json){
        //清空表格
        $('#myTable').dataTable().fnClearTable();  
        if(null==json){
          $('#myTable').dataTable().fnClearTable();  
        }else{
           $('#myTable').dataTable().fnAddData(json,true);  //刷下表格
         }  
       }
     },
     deferRender:true,
     Processing:true,
     buttons: [
     {
      extend: 'collection',
      text: 'Export',
      buttons: [ 'csv-flash', 'xls-flash', 'pdf-flash' ]
    }
    ],
          //默认最后一列（最后更新时间）降序排列
          order: [[3, "desc"]],

          //行创建完成后回调
          rowCallback: function (row, data, index) {

          },
          aoColumns: [
          { data : "Id",
          title: "<input type='checkbox' id='allChecked' name='allChecked' onclick='checkAll()'/>",
          orderable: false , 
          render : function(data, type, full, meta) {
            return '<input type="checkbox" value="'+ data + '" name="ids" onclick="checkOne()"/>';
          }},
          { data: "Id",
          title: "ID"
        },
        { data: "TaskName" ,
        title: "任务名称"
      },
      { data: "TaskStatus" ,
      title: "任务状态",
      render: function (data, type, row, meta) {
        if(row.TaskStatus==0){
         return "<p >未开始</p>";
       }else if (row.TaskStatus==1) {
         return"<p>已完成</p>";
       }else if (row.TaskStatus==2) {
         return"<p>运行中</p>";
       }else if (row.TaskStatus==3) {
         return"<p>超时</p>";
       }else if (row.TaskStatus==4) {
         return"<p>异常</p>";
       }else if (row.TaskStatus==5) {
         return"<p>就绪</p>";
       }else {
         return "<p>等待中</p>";
       }
     }
   },

   { data:"TaskBeginTime",
   title: "开始时间",
   render: function (data, type, row, meta) {

    return getDateTime(data);
  }
},

{ data:"TaskEndTime",
title: "结束时间",
render: function (data, type, row, meta) {

  return getDateTime(data);
}
},
{
  data:"FileTitle",
  title:"文件名"
},
{
  data:"AddTime",
  title:"创建时间",
  render: function (data, type, row, meta) {

    return getDateTime(data);
  }
},


{
  targets:9,
  data: null,
  title: "操作",
  render: function (data, type, row, meta) {

    var href = '';
    href += "&nbsp;  <a href='#/app/stream/stream/edit/"+row.Id+"'>";
    href += "<i  title='编辑' class='fa fa-edit fa-lg'></i></a>";

    href += "&nbsp;  <a href='javascript:void(0);' onclick=goDelete("
    + row.Id
    + ")>";
    href += "<i  title='删除' class='fa fa-trash-o fa-lg'></i></a>";


    return href;

  }
}],
drawCallback: function( settings ) {


},

initComplete:function(data){

},
Paginate: true, 
language:{
 url: 'vendor/jquery/datatables/Chinese.json'
}
});
});  


//单条删除
function goDelete(id){
  top.layer.confirm('您确定删除吗', function(index){        
    var postInfoUrl = API_URL+"task_record";
    var infoUrl = API_URL+"task_record?query=delflag:0,Id:"+id;
    var formData =  MY_ASYNC_AJAX(infoUrl)[0];
    formData.Delflag = "1";
    $.ajax({
      url : postInfoUrl+'/'+id,
      type : 'put',
      data: JSON.stringify(formData),
      cache : false,
      success : function(data) {
        top.layer.msg('删除成功');
        $('#myTable').DataTable().ajax.reload(); 
      }
    });
    top.layer.close(index);
  });
}
</script>







